package rsj.dao;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Date;

import org.apache.commons.lang3.StringUtils;

import rsj.table.Log;

import com.rlyy.aibase.db.QueryRunner;
import com.rlyy.aibase.utils.base.MapNode;
import com.rlyy.aibase.utils.base.Node;
import com.rlyy.aibase.utils.base.TableNode;
import com.rlyy.aibase.utils.helpers.DateHelper;

public class LogInfoDao {
	
	private static final LogInfoDao infoDao = new LogInfoDao();
	
	 public static LogInfoDao getInstance(){
	        return infoDao;
	 }
	 
	 public MapNode findLogList(Connection conn,String userId,int size,int pcount) throws SQLException{
		 
		 QueryRunner queryRunner = new QueryRunner();
	     MapNode responsedata = new MapNode(); 
	     Node node = null;
	     TableNode map = null;
		 StringBuffer countsql = new StringBuffer();
		 countsql.append("select count(1) from "+Log.Table_Name+" where "+Log.Log_Level+" = '1'");
		 if(StringUtils.isNotBlank(userId)){
			 countsql.append(" and "+Log.Log_User_Id+" = ?");
			 node = queryRunner.queryLinkNode(conn, countsql.toString(),userId);
		 }else{
			 node = queryRunner.queryLinkNode(conn, countsql.toString());
		 }
		  	
	     int count = node.valueInt();
	     responsedata.putNode("Log_Count", String.valueOf(count));
	     if(count == 0){  		
	    	 responsedata.putNode("Log_List", "");
	    	 return responsedata;	    		
	     }
	     StringBuffer findsql = new StringBuffer();
	     findsql.append("select "+Log.Log_Id+","+Log.Log_User_Id+","+Log.Log_Ip+","+Log.Log_Location+","+Log.Log_Operation+","+Log.Log_Ctime)
	     		.append(" from "+Log.Table_Name+" where "+Log.Log_Level+" = '1'");
	     if(StringUtils.isNotBlank(userId)){
	    	 findsql.append(" and "+Log.Log_User_Id+" = ? " );
	     }
	     findsql.append(" order by "+Log.Log_Ctime+" desc limit ? Offset ?");
	     
	     if(StringUtils.isNotBlank(userId)){
	    	 map = queryRunner.queryLinkTableNode(conn,findsql.toString(),userId,size,size*(pcount-1));
	     }else{
	    	 map = queryRunner.queryLinkTableNode(conn,findsql.toString(),size,size*(pcount-1));
	     }
	     
		 responsedata.putNode("Log_List", map.toJson());
			
		 return responsedata;
	     
	 }
	 
	 public void insertLog(Connection conn,String id,String userId,String userName,String ip,String location,String operation,String level,String content){
		 QueryRunner queryRunner = new QueryRunner();
		 StringBuffer insertsql = new StringBuffer();
		 insertsql.append("insert into "+Log.Table_Name+"("+Log.Log_Id+","+Log.Log_User_Id+","+Log.Log_User_Name)
		 			.append(","+Log.Log_Ip+","+Log.Log_Location+","+Log.Log_Operation+","+Log.Log_Level)
		 			.append(","+Log.Log_Content+","+Log.Log_Ctime+")")
		 			.append(" values(?,?,?,?,?,?,?,?,?)");
		 queryRunner.update(conn,insertsql.toString(),id,userId,userName,ip,location,operation,level,content,DateHelper.strToTimestamp(DateHelper.getDate(new Date())));
		 
	 }

}
